import pymongo
import random
import datetime

# 连接MongoDB
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["test"]
collection = db["user_reading_data"]
collection.delete_many({})
# 模拟20个用户的阅读数据
for i in range(20):

    # 模拟已读书籍数量
    total_books = random.randint(50, 200)

    # 模拟阅读天数
    reading_days = random.randint(30, 365)

    # 模拟阅读时长（小时）
    total_hours = random.randint(100, 1000)

    # 模拟连续阅读天数
    current_streak = random.randint(0, 30)

    # 模拟书籍趋势（百分比）
    books_trend = random.randint(-20, 20)

    # 模拟时间趋势（百分比）
    time_trend = random.randint(-20, 20)

    # 模拟当前月份
    current_year = random.randint(2023, 2025)
    current_month = random.randint(1, 12)
    current_month_str = f"{current_year}年{current_month}月"

    # 模拟效率评分
    efficiency_score = random.randint(0, 100)

    # 模拟专注度评分
    concentration_score = round(random.uniform(0, 10), 1)

    # 模拟理解度评分
    comprehension_score = round(random.uniform(0, 10), 1)

    # 模拟阅读速度（字/分钟）
    speed_score = random.randint(100, 800)

    # 模拟书籍类型分布
    genre_data = [
        {"name": "文学小说", "value": random.randint(0, total_books), "percentage": 0},
        {"name": "人文社科", "value": random.randint(0, total_books), "percentage": 0},
        {"name": "科技互联网", "value": random.randint(0, total_books), "percentage": 0},
        {"name": "生活艺术", "value": random.randint(0, total_books), "percentage": 0}
    ]

    # 计算书籍类型分布的百分比
    total = sum(item["value"] for item in genre_data)
    for item in genre_data:
        item["percentage"] = round((item["value"] / total) * 100, 1)

    # 模拟阅读时段分布
    time_data = [
        {"time": "早晨(6-12)", "value": random.randint(0, 100), "percentage": 0},
        {"time": "下午(12-18)", "value": random.randint(0, 100), "percentage": 0},
        {"time": "晚上(18-24)", "value": random.randint(0, 100), "percentage": 0},
        {"time": "深夜(0-6)", "value": random.randint(0, 100), "percentage": 0}
    ]

    # 计算阅读时段分布的百分比
    total_time = sum(item["value"] for item in time_data)
    for item in time_data:
        item["percentage"] = round((item["value"] / total_time) * 100, 1)

    # 构建用户数据文档
    user_data = {
        "u_id": i+1,
        "total_books": total_books,
        "reading_days": reading_days,
        "total_hours": total_hours,
        "current_streak": current_streak,
        "books_trend": books_trend,
        "time_trend": time_trend,
        "current_month": current_month_str,
        "efficiency_score": efficiency_score,
        "concentration_score": concentration_score,
        "comprehension_score": comprehension_score,
        "speed_score": speed_score,
        "genre_data": genre_data,
        "time_data": time_data,
        "last_update": datetime.datetime.now()
    }

    # 将用户数据插入到MongoDB集合中
    collection.insert_one(user_data)

print("模拟数据已成功保存到MongoDB中。")